package com.linln.agent.member;

import com.linln.common.constant.AdminConst;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.AgentContact;
import com.linln.modules.member.domain.AreaCount;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.service.AgentContactService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 小懒虫
 * @date 2021/03/09
 */
@Controller
@RequestMapping("agent/member/agentContact")
public class AgentContactController {

    @Autowired
    private AgentContactService agentContactService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("memberAgentContact:agentContact:index")
    public String index(Model model, AgentContact agentContact) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();

        // 获取数据列表
        Example<AgentContact> example = Example.of(agentContact, matcher);
        Page<AgentContact> list = agentContactService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/memberAgentContact/agentContact/index";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit")
    public String toEdit(HttpServletRequest request, Model model) {
        MemberAgent agent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME);
        AgentContact  agentContact=agentContactService.getByAgentId(agent.getId());
        model.addAttribute("agentContact", agentContact);
        return "/agent/member/agentContact/add";
    }

    /**
     * 保存添加/修改的数据
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVo save(HttpServletRequest request,AgentContact agentContact) {
        // 复制保留无需修改的数据
        if (agentContact.getId() != null) {
            AgentContact beAgentContact = agentContactService.getById(agentContact.getId());
            beAgentContact.setPhone(agentContact.getPhone());
            beAgentContact.setWxname(agentContact.getWxname());
        }else{
            MemberAgent agent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME);
            agentContact.setAgentId(agent.getId());
            agentContact.setMemberId(agent.getMember().getId());
            agentContact.setAgentId(agent.getId());
            agentContact.setArea(agent.getArea());
        }
        // 保存数据
        agentContactService.save(agentContact);
        return ResultVoUtil.SAVE_SUCCESS;
    }

}